package com.example.costumview.customview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.costumview.R;

public class ShadowLayerView extends View {
    private Paint mPaint=new Paint();
    private Bitmap mDogmBmp;
    private boolean mShowShadow;

    private float mRadius=1;

    private float mDx=10,mDy=10;

    public ShadowLayerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setLayerType(LAYER_TYPE_SOFTWARE,null);
        mPaint.setColor(Color.BLACK);
        mPaint.setTextSize(25);

        mDogmBmp= BitmapFactory.decodeResource(getResources(), R.drawable.dog);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mShowShadow){
            mPaint.setShadowLayer(mRadius,mDx,mDy,Color.GRAY);
        }else{
            mPaint.clearShadowLayer();
        }

        canvas.drawColor(Color.WHITE);
        canvas.drawText("旗舰",100,100,mPaint);
        canvas.drawCircle(300,100,50,mPaint);
        canvas.drawBitmap(mDogmBmp,null,new Rect(100,200,100+mDogmBmp.getWidth(),200+mDogmBmp.getHeight()),mPaint);
    }

    public void setShadow(boolean showShadow){
        mShowShadow = showShadow;
        postInvalidate();
    }
}
